package bma.common.langutil.ai.future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import bma.common.langutil.concurrent.FutureListener;
import bma.common.langutil.concurrent.ListenableFuture;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 * @param <RESP_TYPE>
 */
public class AIFSimple<RESP_TYPE> implements ListenableFuture<RESP_TYPE> {

	private RESP_TYPE value;
	private Throwable throwable;

	public AIFSimple(RESP_TYPE value, Throwable throwable) {
		super();
		this.value = value;
		this.throwable = throwable;
	}

	@Override
	public boolean cancel(boolean mayInterruptIfRunning) {
		return true;
	}

	@Override
	public boolean isCancelled() {
		return false;
	}

	@Override
	public boolean isDone() {
		return true;
	}

	@Override
	public RESP_TYPE get() throws InterruptedException, ExecutionException {
		if (throwable != null) {
			if (throwable instanceof ExecutionException) {
				ExecutionException e = (ExecutionException) throwable;
				throw e;
			}
		}
		return this.value;
	}

	@Override
	public RESP_TYPE get(long timeout, TimeUnit unit)
			throws InterruptedException, ExecutionException, TimeoutException {
		return get();
	}

	@Override
	public void addListener(FutureListener<RESP_TYPE> listener) {
		// if (executor == null)
		// executor = AIUtil.sameThreadExecutor();
		// executor.execute(listener);
	}

	@Override
	public void removeListener(FutureListener<RESP_TYPE> listener) {
		// TODO Auto-generated method stub

	}

}
